Draft
Conversation
6889fce to
412bc3d
Compare
Lost commits from 4bd79e3...0e4ae38 in the squash. This readds them.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add inline type annotations
Important
We want feedback from people who actively maintain projects that depend on Requests or use it heavily. Please share your experience testing this against your code in the linked issue.
Comments that are clearly AI-generated will be hidden or removed. This is a library written "for Humans". The conversation is between maintainers and users, not a prompt.
Overview
This PR adds type annotations throughout Requests to codify the type contract alongside the code. This is a path for what inline typing in Requests could look like. The goals are to establish what APIs are intended to support, make more reasoned decisions about code changes, and make using Requests in a modern development environment easier.
The package passes strict type checking with pyright, with the caveat that a few decisions on internals have been deferred for discussion by the maintainers and broader community. Right now, the intended behavior for those is undefined.
Related issue: RFC: Adding inline type annotations to Requests
How to verify locally
Should report 0 errors.
typing-extensionsis needed on Python 3.10-3.12.Main design decisions
cast()overassert/isinstancefor type narrowing to avoid breaking downstream code at runtime_types.pymodule centralizing Protocols, type aliases, and TypeVarsSupportsRead/SupportsItemsas@runtime_checkableProtocols replacinghasattrchecksis_prepared()as aTypeIsguard that's a no-op at runtime (always returnsTrue)Runtime impact
A full catalog of every runtime-impacting change is available here. The behavioral changes and bug fixes are the ones worth scrutinizing. The rest is mechanical.
Open discussion points
strvsbytesURL handling: Requests has historically accepted both when Python 2.7 was primary usage. These types currently declarestronly. This needs a decision on whether to fix existing broken bytes behavior or formalize thestr-only contract.yield_requestsAPI: Needs documentation and a decision on how this should even work in practice. This API decision came through without review and I don't think there's any realistic usage outside of the library.What's NOT in this PR